package br.indt.framework.ui.common;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.indt.framework.log.MicrologWrapper;
import br.indt.framework.ui.ImageLoader;

public class OptionMenuItemFullTouch extends OptionMenuItem {
	
	private static final MicrologWrapper log = MicrologWrapper.getLogger(OptionMenuItemFullTouch.class);
	
	private Image top = null;
	private Image left = null;
	private boolean isLastItem = false;
	private boolean isFirstItem = false;
	private boolean contextual = false;

	public OptionMenuItemFullTouch(String option, MenuActionInterface pAction) {
		super(option, pAction);

		label = new Label("l1", option, true, 1, null);
		label.setTextColor(0x333333);
		label.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
		label.setMargin(20, 0, 0, 0);

		try {
			divisorImage = ImageLoader.load(ImageLoader.MENU_ITEM_DIVISOR);
			top = ImageLoader.load(ImageLoader.MENU_ITEM_TOP);
			left = ImageLoader.load(ImageLoader.MENU_ITEM_LEFT);
		} catch (Exception e) {
			log.error("OptionMenuItemFullTouch::OptionMenuItem " + e);
		}

		calculateHeight();
	}

	public void paint(Graphics g, int x, int y, int maxWidth, int maxHeight) {
		int width = maxWidth - (rightMargin + leftMargin);

		
		g.setClip(0, y, maxWidth, height);

		if (pressedDown) {
			g.setColor(Color.BLACK);
			label.setTextColor(Color.WHITE);
		} else {
			g.setColor(getBackgroundColor());
			label.setTextColor(0x333333);
		}
		

		y += bottomMargin;
		x += leftMargin + 5;
		width -= 10;
		
		try {
			
			if(isFirstItem()){
				if(contextual){
					g.drawImage(left, x + 1, y + 3, Graphics.TOP | Graphics.LEFT);
					x+=6;
					g.fillRoundRect(x , y, width - 6, height, 7, 7);
				}else{
					g.drawImage(top, x, y, Graphics.TOP | Graphics.LEFT);
					y+=6;
					g.fillRoundRect(x , y, width, height, 0, 0);
				}
			}else if(isLastItem()){
				if(contextual){
					x+=6;
					g.fillRoundRect(x , y, width - 6, height, 7, 7);
					g.drawImage(divisorImage, x, y, Graphics.TOP | Graphics.LEFT);
				}else{
					g.fillRoundRect(x , y, width, height, 7, 7);
					g.fillRoundRect(x , y, width, 20, 0, 0);
					g.drawImage(divisorImage, x, y, Graphics.TOP | Graphics.LEFT);
					g.drawImage(divisorImage, x, y + 38, Graphics.TOP | Graphics.LEFT);
				}
			}else{
				g.fillRoundRect(x , y, width, height, 0, 0);
				g.drawImage(divisorImage, x, y , Graphics.TOP | Graphics.LEFT);
			}
			
			label.paint(g, x, y+((height/2)-(label.getHeight()/2)), width, maxHeight);
			
		} catch (Exception e) {
			//empty
		}
	}

	public void click(int x, int y) {
		try {
			Display.getInstance().setControllerListener(true);
			Display.getInstance().pop();

		} catch (Exception e) {
			log.error("OptionMenuItemFullTouch::click " + e);
		}

		action.menuOptionAction(this);
		setPressedDownState(false);
	}

	public void pressFire() {
		click(0, 0);
	}
	
	public boolean isLastItem() {
		return isLastItem;
	}

	public void setLastItem(boolean isLastItem) {
		this.isLastItem = isLastItem;
	}
	
	public boolean isFirstItem() {
		return isFirstItem;
	}

	public void setFirstItem(boolean isFirstItem) {
		this.isFirstItem = isFirstItem;
	}
	
	public boolean isContextual() {
		return contextual;
	}

	public void setContextual(boolean contextual) {
		this.contextual = contextual;
	}
}